package com.yonyou.cloud.member.domainmodel.services.impl;

import com.yonyou.cloud.member.domainmodel.context.ServiceContext;
import com.yonyou.cloud.member.domainmodel.dto.MemberPointHistoryDto;
import com.yonyou.cloud.member.domainmodel.dto.MemberPointQueryConditionDto;
import com.yonyou.cloud.member.domainmodel.dto.PageQueryResult;
import com.yonyou.cloud.member.domainmodel.entities.member.Member;
import com.yonyou.cloud.member.domainmodel.vo.MemberId;
import com.yonyou.cloud.member.domainmodel.repositories.MemberPointRepository;
import com.yonyou.cloud.member.domainmodel.repositories.MemberRepository;
import com.yonyou.cloud.member.domainmodel.services.MemberService;
import com.yonyou.cloud.member.exceptions.BusinessException;
import com.yonyou.cloud.member.exceptions.ParameterFormatException;
import com.yonyou.cloud.member.exceptions.PropertyValueRequiredException;
import com.yonyou.cloud.member.libs.key.KeyGenerator;
import com.yonyou.cloud.member.libs.util.DateTimeUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.List;
import java.util.regex.Pattern;

@Service
public class MemberServiceImpl implements MemberService {
    @Autowired
    MemberRepository memberRepository;
    @Autowired
    MemberPointRepository pointRepository;
    @Autowired
    KeyGenerator keyGenerator;
    Pattern phonePattern = Pattern.compile("^1\\d{10}$");

    @Override
    public Member find(MemberId id) {
        Member member = memberRepository.find(id);
        if (member == null) {
            throw new BusinessException("会员不存在");
        }
        return member;
    }

    @Override
    public Member find(String phone) {
        // 参数校验
        if (!phonePattern.matcher(phone).find()) {
            throw new ParameterFormatException("手机号码格式不正确");
        }
        Member member = memberRepository.find(phone);
        if (member == null) {
            throw new BusinessException("会员不存在");
        }
        return member;
    }

    @Override
    public List<Member> list(String level) {
        return memberRepository.list(level);
    }

    @Override
    public int count() {
        return memberRepository.count();
    }

    @Override
    public boolean add(Member member) {
        // 设置属性值：默认值，上下文中的值
        member.setId(keyGenerator.generate(MemberId.class));
        member.setCreateTime(DateTimeUtils.fromTimestamp(member.getId().ts()));
        if (StringUtils.isEmpty(member.getLevel())) {
            member.setLevel("青铜");
        }
        // 属性校验
        propertyCheck(member);
        // 逻辑校验
        // 逻辑处理
        // 持久化处理
        return memberRepository.insert(member);
    }

    private void propertyCheck(Member member) {
        if (StringUtils.isEmpty(member.getPhone())) {
            throw new PropertyValueRequiredException("请输入会员手机号码");
        }
        if (StringUtils.isEmpty(member.getName())) {
            throw new PropertyValueRequiredException("请输入会员手机号码");
        }
        if (!phonePattern.matcher(member.getPhone()).find()) {
            throw new PropertyValueRequiredException("会员手机号码格式不正确");
        }
    }

    @Override
    public boolean delete(MemberId id) {
        return memberRepository.delete(id);
    }

    @Override
    public PageQueryResult<MemberPointHistoryDto> queryMemberPointHistory(MemberPointQueryConditionDto conditionDto, ServiceContext serviceContext) {
        return pointRepository.query(conditionDto.getMemberId(), conditionDto.getTimeRange(), conditionDto.getPageInfo());
    }
}